// packageF/others/customerCenter/customerCenterRecord/customerCenterRecord.js
const app = getApp();
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    startTime: '',
    endtTime: '',
    list: [],
    customer_details: [],
    display_set: [],

    //more
    isLoadMore: true,
    total_page: 0,
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let time = this.formatTimes(new Date());
    this.setData({
      startTime: time,
      endtTime: this.formatTimes(this.getlastMonth())
    })
    wx.setNavigationBarTitle({
      title: '轨迹追踪',
    })
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
    this.getData();
  },
  confirm(e) {
    console.log(e);
    this.setData({
      endtTime: e.detail.value
    })
    this.init();
    this.getData();
  },
  confirm_end(e) {
    this.setData({
      startTime: e.detail.value
    })
    this.init();
    this.getData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  telCall(e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  clearTime() {
    this.setData({
      endtTime: '',
      startTime: ''
    })
    this.init();
    this.getData();
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      list: []
    })
  },
  onCopyWechat(e) {
    let wechat = e.currentTarget.dataset.wechat;
    wx.setClipboardData({
      data: wechat,
      success: () => {
        wx.hideToast()
        wx.showModal({
          content: '复制微信号成功',
          showCancel: false
        })
      }
    })
  },
  isUrl(value) {
    return !/^((https|http):\/\/).+$/g.test(value);
  },
  onCopy(e) {
    let url = e.currentTarget.dataset.url;
    if (!this.isUrl(url)) {
      wx.setClipboardData({
        data: 'data',
        success: () => {
          wx.hideToast()
          wx.showModal({
            content: '已经复制链接，请在微信聊天窗口中发送后点击打开！',
            showCancel: false
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '/' + url,
      })
    }

  },
  formatTimes(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    return [year, month, day].map(this.formatNumber).join('-')
  },
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  getlastMonth() {
    let now = new Date();
    // 当前月的日期
    let nowDate = now.getDate();
    let lastMonth = new Date(now.getTime());
    // 设置上一个月（这里不需要减1）
    lastMonth.setMonth(lastMonth.getMonth());
    // 设置为0，默认为当前月的最后一天
    lastMonth.setDate(0);
    // 上一个月的天数
    let daysOflastMonth = lastMonth.getDate();
    // 设置上一个月的日期，如果当前月的日期大于上个月的总天数，则为最后一天 
    lastMonth.setDate(nowDate > daysOflastMonth ? daysOflastMonth : nowDate);
    console.log(lastMonth)
    return lastMonth;
  },
  getData() {
    let json = {
      member_id: this.data.id
    };
    if (this.data.endtTime) {
      json.start_date = this.data.endtTime;
    }
    if (this.data.startTime) {
      json.end_date = this.data.startTime;
    }
    let urlStr = app.getNetAddresss('plugin.customer-center.frontend.controllers.browse.get-member-browse-details');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.browse_details_list.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            customer_details: res.data.customer_details,
            list: res.data.browse_details_list.data,
            display_set: res.data.display_set
          })

        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let json = {
        member_id: this.data.id
      };
      if (this.data.endtTime) {
        json.start_date = this.data.endtTime;
      }
      if (this.data.startTime) {
        json.end_date = this.data.startTime;
      }
      this.data.page = this.data.page + 1;
      json.page = this.data.page;
      urlStr = app.getNetAddresss('plugin.customer-center.frontend.controllers.browse.get-member-browse-details');
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              list: this.data.list.concat(res.data.browse_details_list.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})